<template>
  <a-layout :class="['admin-layout', 'beauty-scroll']">
    <div class="side-menu-contain" v-if="!POWERED_BY_QIANKUN">
      <side-menu
        :class="[fixedSideBar ? 'fixed-side' : '']"
        :theme="theme.mode"
        :menuData="sideMenuData"
        :collapsed="collapsed"
        :collapsible="true"
        @toggleCollapse="toggleCollapse"
      />
      <div
        v-if="fixedSideBar"
        :style="`width: ${sideMenuWidth}; min-width: ${sideMenuWidth};max-width: ${sideMenuWidth};`"
        class="virtual-side"
      ></div>
    </div>
    <a-layout class="admin-layout-main beauty-scroll">
      <div class="tabs-header">
        <admin-header
          v-if="!POWERED_BY_QIANKUN"
          :class="[
            {
              'fixed-tabs': fixedTabs,
              'fixed-header': fixedHeader,
              'multi-page': multiPage
            }
          ]"
          :style="headerStyle"
          :menuData="headMenuData"
          :collapsed="collapsed"
          @toggleCollapse="toggleCollapse"
        />
        <slot name="tabs-list"> </slot>
      </div>
      <a-layout-header
        :class="[
          'virtual-header',
          {
            'fixed-tabs': fixedTabs,
            'fixed-header': fixedHeader,
            'multi-page': multiPage
          }
        ]"
        v-show="fixedHeader"
      ></a-layout-header>
      <a-layout-content
        class="admin-layout-content"
        :style="`min-height: ${minHeight}px;`"
      >
        <div style="position: relative">
          <slot></slot>
        </div>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script>
/* eslint-disable  */
import AdminHeader from "./header/AdminHeader";
import SideMenu from "../components/menu/SideMenu";
import { mapState, mapMutations, mapGetters } from "vuex";

export default {
  name: "AdminLayout",
  components: { SideMenu, AdminHeader },
  data() {
    return {
      minHeight: window.innerHeight - 82,
      collapsed: true,
      showSetting: false,
      drawerOpen: false,
      POWERED_BY_QIANKUN: window.__POWERED_BY_QIANKUN__
    };
  },
  provide() {
    return {
      adminLayout: this
    };
  },
  watch: {
    $route(val) {
      this.setActivated(val);
    },
    layout() {
      this.setActivated(this.$route);
    },
    isMobile(val) {
      if (!val) {
        this.drawerOpen = false;
      }
    }
  },
  computed: {
    ...mapState("setting", [
      "isMobile",
      "theme",
      "layout",
      "footerLinks",
      "copyright",
      "fixedHeader",
      "fixedSideBar",
      "fixedTabs",
      "hideSetting",
      "multiPage"
    ]),
    ...mapGetters("setting", ["firstMenu", "subMenu", "menuData"]),
    sideMenuWidth() {
      return this.collapsed ? "116px" : "280px";
    },
    headerStyle() {
      let width =
        this.fixedHeader && this.layout !== "head" && !this.isMobile
          ? `calc(100% - ${this.sideMenuWidth})`
          : "100%";
      let position = this.fixedHeader ? "fixed" : "static";
      return `width: ${width}; position: ${position};`;
    },
    headMenuData() {
      const { layout, menuData, firstMenu } = this;
      return layout === "mix" ? firstMenu : menuData;
    },
    sideMenuData() {
      const { layout, menuData, subMenu } = this;
      return layout === "mix" ? subMenu : menuData;
    }
  },
  methods: {
    ...mapMutations("setting", ["correctPageMinHeight", "setActivatedFirst"]),
    toggleCollapse() {
      this.collapsed = !this.collapsed;
    },
    onMenuSelect() {
      this.toggleCollapse();
    },
    setActivated(route) {
      if (this.layout === "mix") {
        let matched = route.matched;
        matched = matched.slice(0, matched.length - 1);
        const { firstMenu } = this;
        for (let menu of firstMenu) {
          if (matched.findIndex((item) => item.path === menu.fullPath) !== -1) {
            this.setActivatedFirst(menu.fullPath);
            break;
          }
        }
      }
    }
  },
  created() {
    this.correctPageMinHeight(this.minHeight - 24);
    this.setActivated(this.$route);
  },
  beforeDestroy() {
    this.correctPageMinHeight(-this.minHeight + 24);
  }
};
</script>

<style lang="less" scoped>
.admin-layout {
  .side-menu {
    &.fixed-side {
      background: #eeeef1;
      position: fixed;
      height: 100vh;
      left: 0;
      top: 0;
    }
  }
  .virtual-side {
    background: #eeeef1;
    transition: all 0.2s;
    border-right: 1px solid #f0f0f0;
  }
  .virtual-header {
    transition: all 0.2s;
    opacity: 0;
    &.fixed-tabs.multi-page:not(.fixed-header) {
      height: 0;
    }
  }
  .admin-layout-main {
    .admin-header {
      top: 0;
      right: 0;
      overflow: hidden;
      transition: all 0.2s;
      &.fixed-tabs.multi-page:not(.fixed-header) {
        height: 0;
      }
    }
  }
  .admin-layout-content {
    z-index: 3;
    padding: 24px 0 0;
    /*overflow-x: hidden;*/
    /*min-height: calc(100vh - 64px - 122px);*/
  }
  .setting {
    background-color: @primary-color;
    color: @base-bg-color;
    border-radius: 5px 0 0 5px;
    line-height: 40px;
    font-size: 22px;
    width: 40px;
    height: 40px;
    box-shadow: -2px 0 8px @shadow-color;
  }
  .tabs-header {
    position: relative;
    // border-bottom: 1px solid #f0f0f0;  // TODO 需不需要
  }

  .side-menu-contain {
    background: #eeeef1;
  }
}

.open-btn {
  width: 280px;
  padding-left: 2%;
}

.shut-btn {
  width: 80px;
  text-align: center;
}
</style>
